﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;

namespace LoginServer
{
    public partial class ManagerUserConnection
    {
        void Process_L_Login(object[] param)
        {
            #region release
            User user = convert_data.GetUserConnection(param);           
            string username="";
            string pass="";
            convert_data.ConvertReceive_L_Login(ref username, ref pass, param);
            PlayerInfo player = login_server.AccessDB.Login(username, pass, true);
            if (player == null)
                user.SendMessage_L_Login_Failed("Tên đăng nhập hoặc password không chính xác");
            else if(CheckUserNotIn(player.ID))
            {
                user.info = player;
                user.info.friends = login_server.AccessDB.GetFriendsOfUser(user.User_ID);
                Users.Add(user.User_ID, user);
                //Console.WriteLine("User ID = {0}  Login OK", user.User_ID);
                user.SendMessage_L_Login_OK(user.User_ID);
                foreach (var i in user.info.friends)
                {
                    if (!CheckUserNotIn(i.ID))
                    {
                        this.GetUser(i.ID).SendMessage_L_Update_Friend_OK(user.User_ID);
                    }
                }
                NotifyUser(true, username, player.ID);
            }
            #endregion
        }

        void NotifyUser(bool online, string name, int id)
        {
            try
            {
                if (online == true)
                {
                    byte[] data = Encoding.UTF8.GetBytes(name + ":" + id.ToString());
                    List<byte> tmp = new List<byte>() { (byte)data.Length };
                    tmp.AddRange(data);
                    SendMessageToAllExceptID(id, MSG_GSM.L_User_On, tmp.ToArray());
                }
                else
                {
                    byte[] data = Encoding.UTF8.GetBytes(name);
                    List<byte> tmp = new List<byte>() { (byte)data.Length };
                    tmp.AddRange(data);
                    SendMessageToAllExceptID(id, MSG_GSM.L_User_Off, tmp.ToArray());
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Notify error:"+e.Message);
            }
        }

        //chat

        void Process_L_Chat_Channel(object[] param)
        {
            string[] res = convert_data.ConvertReceive_L_Chat_Channel(param);
            switch ((string)res[0])
            {
                case "World":
                    {
                        foreach (User us in Users.Values)
                            us.SendMessage_L_Chat_Channel(res[0] + ":" + res[1] + ":" + res[2]);
                    }
                    break;
                case "Duel":
                    {
                        foreach (User us in Users.Values)
                            us.SendMessage_L_Chat_Channel(res[0] + ":" + res[1] + ":" + res[2]);
                    }
                    break;
                case "Trade":
                    {
                        foreach (User us in Users.Values)
                            us.SendMessage_L_Chat_Channel(res[0] + ":" + res[1] + ":" + res[2]);
                    }
                    break;
                case "Friend":
                    {

                        foreach (User u in Users.Values)
                            if (u.info.NickName==res[1])
                            {
                                foreach (PlayerInfo friend in u.info.friends)
                                {
                                    User us = (User)Users[friend.ID];
                                    if (us != null)
                                        us.SendMessage_L_Chat_Channel(res[0] + ":" + res[1] + ":" + res[2]);
                                }
                                break;
                            }                     
                    }
                    break;
                case "Room":
                    {
                        int room = 0;
                        foreach (User u in Users.Values)
                        {
                            if (u.info.NickName == res[1])
                                room = u.info.RoomID;
                            break;
                        }
                        foreach (User us in Users.Values)
                        {
                            if (us.info.RoomID == room)
                                us.SendMessage_L_Chat_Channel(res[0] + ":" + res[1] + ":" + res[2]);
                        }
                    }
                    break;
                case "Group":
                    {

                    }
                    break;
            }


        }
        void Process_L_Chat_OK(object[] param)
        {
            string[] res = convert_data.ConvertReceive_L_Chat_OK(param);
            User[] us = new User[Users.Values.Count];

            Users.Values.CopyTo(us, 0);
            User chatus = us[0];
            //for (int i=0;i<us.Length;i++)
            //    if (res[0] == us[i].User_Name)
            //    {
            //        chatus = us[i];
            //        break;
            //    }
            for (int i = 0; i < us.Length; i++)
            {
                if (res[1] == us[i].User_Name)
                {
                    us[i].SendMessage_L_Chat_OK(res[0] + ":" + res[1] + ":" + res[2]);
                    return;
                }
            }
            us[0].SendMessage_L_Chat_Error(res[1] + ":" + res[0] + ":" + res[1] + " is offline !");
        }

        void Process_L_User_AllOn(object[] param)
        {
            string id = convert_data.ConvertReceive_L_User_AllOn(param);
            string s = "";
            foreach (User us in Users.Values)
            {
                if (us.User_ID.ToString() == id)
                {
                    s += us.User_Name + ":";
                    break;
                }
            }
            foreach (User us in Users.Values)
            {
                if (us.User_ID.ToString() == id)
                    continue;
                s += us.User_Name + ":";
            }
            byte[] data = Encoding.UTF8.GetBytes(s);
            List<byte> tmp = new List<byte>() { (byte)data.Length };
            tmp.AddRange(data);
            SendMessageToUserID(int.Parse(id), MSG_GSM.L_User_AllOn, tmp.ToArray());
        }
        
        
    }
}
